﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.Profile;

namespace HRMService
{
    public partial class MainService : IRoleRight
    {
        public DataSet GetAllModule()
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_Modules_GetAllModules", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet GetAllByRole(string RoleID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_RoleRights_GetAllByRole", dbConn);
            dbCmd.Parameters.AddWithValue("@RoleID", RoleID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Delete(string RoleID)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_RoleRights_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@RoleID", RoleID);
            dbCmd.CommandType = CommandType.StoredProcedure;


            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Update(string RoleID, string ModuleID, bool ViewRight, bool CreateRight, bool UpdateRight, bool PrintRight, bool DeleteRight)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_RoleRights_Update", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@RoleID", RoleID);
            dbCmd.Parameters.AddWithValue("@ModuleID", ModuleID);
            dbCmd.Parameters.AddWithValue("@ViewRight", ViewRight);
            dbCmd.Parameters.AddWithValue("@CreateRight", CreateRight);
            dbCmd.Parameters.AddWithValue("@PrintRight", PrintRight);
            dbCmd.Parameters.AddWithValue("@DeleteRight", DeleteRight);
            dbCmd.Parameters.AddWithValue("@UpdateRight", UpdateRight);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public bool Insert(string RoleID, string ModuleID, bool ViewRight, bool CreateRight, bool UpdateRight, bool PrintRight, bool DeleteRight)
        {
            bool retVal = false;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_RoleRights_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@RoleID", RoleID);
            dbCmd.Parameters.AddWithValue("@ModuleID", ModuleID);
            dbCmd.Parameters.AddWithValue("@ViewRight", ViewRight);
            dbCmd.Parameters.AddWithValue("@CreateRight", CreateRight);
            dbCmd.Parameters.AddWithValue("@PrintRight", PrintRight);
            dbCmd.Parameters.AddWithValue("@DeleteRight", DeleteRight);
            dbCmd.Parameters.AddWithValue("@UpdateRight", UpdateRight);
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }

        public DataSet GetAllByRoleandModule(string RoleID, string ModuleID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = new SqlConnection(Properties.Settings.Default.ConnectionString);
            SqlCommand dbCmd = new SqlCommand("HRM_RoleRights_GetAllByRoleandModule", dbConn);
            dbCmd.Parameters.AddWithValue("@RoleID", RoleID);
            dbCmd.Parameters.AddWithValue("@ModuleID", ModuleID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        
        public RoleRightInfo RoleRight_GetRoleRight(string UserName, string ModuleID)
        {
            RoleRightInfo retVal = new RoleRightInfo();
            string[] roles = Roles.GetRolesForUser(UserName);
            foreach (string RoleID in roles)
            {
                DataTable dt = GetAllByRoleandModule(RoleID, ModuleID).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    if (Convert.ToBoolean(dt.Rows[0]["CreateRight"]))
                        retVal.Create = Convert.ToBoolean(dt.Rows[0]["CreateRight"]);
                    if (Convert.ToBoolean(dt.Rows[0]["UpdateRight"]))
                        retVal.Update = Convert.ToBoolean(dt.Rows[0]["UpdateRight"]);
                    if (Convert.ToBoolean(dt.Rows[0]["PrintRight"]))
                        retVal.Print = Convert.ToBoolean(dt.Rows[0]["PrintRight"]);
                    if (Convert.ToBoolean(dt.Rows[0]["DeleteRight"]))
                        retVal.Delete = Convert.ToBoolean(dt.Rows[0]["DeleteRight"]);
                    if (Convert.ToBoolean(dt.Rows[0]["ViewRight"]))
                        retVal.View = Convert.ToBoolean(dt.Rows[0]["ViewRight"]);
                }
            }
            return retVal;
        }
    }
}
